package com.fzu.express.controller;


import com.fzu.express.annotation.SystemLog;
import com.fzu.express.enums.RoleEnum;
import com.fzu.express.pojo.PasswordDTO;
import com.fzu.express.pojo.RegisterDTO;
import com.fzu.express.pojo.UserDTO;
import com.fzu.express.pojo.UserVO;
import com.fzu.express.service.WebUserService;
import com.fzu.express.utils.JwtUtil;
import com.fzu.express.utils.R;
import io.jsonwebtoken.Jwt;
import org.springframework.web.bind.annotation.*;
import com.fzu.express.entity.WebUser;
import com.fzu.express.service.WebUserService;
import com.fzu.express.utils.PageVO;
import com.fzu.express.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2022-10-27
 */
@RestController
@RequestMapping("/web-user")
public class WebUserController {

    @Resource
    private WebUserService webUserService;


    /**
     * 登录
     * @param userDTO 账号密码
     * @return R
     */
    @PostMapping("/login")
    public R login(@RequestBody UserDTO userDTO) {
        UserVO login = webUserService.login(userDTO);
        return R.ok().data(login);
    }

    /**
     * 注册
     * @param registerDTO 注册信息
     * @return R
     */
    @PostMapping("/register")
    public R register(@RequestBody @Valid RegisterDTO registerDTO) {
        webUserService.register(registerDTO);
        return R.ok();
    }


    /**
     * 修改密码
     * @param passwordDTO 修改信息
     * @return R
     */
    @PutMapping("/password")
    @SystemLog(businessName = "修改密码")
    public R login(@RequestBody @Valid PasswordDTO passwordDTO, @RequestHeader("token") String token) {
        WebUser user = JwtUtil.getUser(token);
        webUserService.updatePassword(passwordDTO, user);
        return R.ok();
    }

    /**
     * 分页条件查询
     * @param webuser 参数
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @return R
     */
    @GetMapping
    public R list(WebUser webuser, @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                  @RequestHeader("token") String token) {
        WebUser user = JwtUtil.getUser(token);
        PageVO pageVO = webUserService.listWebUser(webuser, user, pageNum, pageSize);
        return R.ok().data(pageVO);
    }


    /**
     * 新增账号
     * @param webuser 参数
     * @return R
     */
    @PostMapping
    @SystemLog(businessName = "新增账号")
    public R add(@RequestBody WebUser webuser, @RequestHeader("token") String token) {
        WebUser currentUser = JwtUtil.getUser(token);
        // 网点管理员只能添加快递员，且自动分配到自己所在的网点
        if (RoleEnum.OUTLET_MANAGER.getCode().equals(currentUser.getRole())) {
            webuser.setRole(RoleEnum.COURIER.getCode());
            webuser.setOutletId(currentUser.getOutletId());
        }
        webUserService.addWebUser(webuser);
        return R.ok();
    }


    /**
     * 修改
     * @param webuser 参数
     * @return R
     */
    @PutMapping
    @SystemLog(businessName = "更新账号")
    public R update(@RequestBody WebUser webuser) {
        webUserService.updateWebUser(webuser);
        return R.ok();
    }


    /**
     * 删除
     * @param id 账号ID
     * @return R
     */
    @DeleteMapping("/{id}")
    @SystemLog(businessName = "删除账号")
    public R delete(@PathVariable("id") Long id) {
        webUserService.deleteWebUser(id);
        return R.ok();
    }




}

